package com.lotter.api.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lotter.api.service.ClickRecordService;
import com.lotter.api.service.HousenoteService;
import com.lotter.api.service.HousernewsService;
import com.lotter.common.enumobject.ClickTypeEunm;
import com.lotter.common.exception.LotterCusException;
import com.lotter.common.util.StringUtil;
import com.lotter.invocation.StaticDataContext;
import com.lotter.model.hoursenew.HouserNews;
import com.lotter.model.housenote.Housenote;

@Controller
@RequestMapping(value="/website/note")
public class HousenoteControlller extends BaseController {
	
	@Autowired
	private HousenoteService housenoteService ;
	

	@Autowired
	private ClickRecordService clickRecordService;
	
	@Autowired
	private HousernewsService housernewsService;
	/**
	 * 列表
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public Map<String,Object> list(@RequestParam(value ="token",required = true) String token,
			@RequestParam(value ="hid",required= true)Long hid) {
		if(hid == null)
			return ajaxLotterStage("参数有误，请重新请求");
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			Long uid = StaticDataContext.onLineUid(token);
			housenoteService.findHousenoteList(result,uid,hid);
		} catch (LotterCusException e) {
			logger.error("查询有误，请检查代码");
			return ajaxLotterServer("查收出错,请联系管理员");
		}
		return result;
	}
	
	
	/**
	 * 保存房间评论
	 * @param request
	 * @param housernews
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/insert",method=RequestMethod.POST)
	public Map<String,Object> insert(@RequestParam(value ="token",required = true) String token,
			@RequestParam(value ="content",required = true) String content,
			@RequestParam(value ="hid",required = true) Long hid) {
		if(StringUtil.isBlank(content))
			return ajaxLotterStage("评论内容必须传入");
		if(hid == null)
			return ajaxLotterStage("新闻ID必须传入");

		try {
			int number  =  content.toCharArray().length;
			if(number > 50){
				return ajaxLotterStage("评论内容在50个字符之内");
			}
			
			HouserNews houserNews =housernewsService.selectByPrimaryKey(hid);
			if(houserNews == null){
				return ajaxLotterStage("没有找到相关新闻");
			}
			Long uid = StaticDataContext.onLineUid(token);
			
			housenoteService.insert(uid, content, hid);
		} catch (Exception e) {
			logger.error("保存房间信息失败,请联系管理员");
			return ajaxLotterServer("保存房间信息失败,请联系管理员");
		}
		return ajaxLotterSuccess();
	}
	
	
	
	/**
	 * 房间新闻顶一下
	 * @param request
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/topnumNote",method = RequestMethod.GET)
	public Map<String,Object> topnumNote(@RequestParam(value ="token",required = true) String token,
			@RequestParam(value = "nid",required=true) Long nid){
		if(nid == null)
			return ajaxLotterStage("参数有误，评论ID 必须传入");
		try {
			Housenote housenote =housenoteService.selectByPrimaryKey(nid);
			if(housenote == null){
				return ajaxLotterStage("没有找到相关评论");
			}
			
			Long uid = StaticDataContext.onLineUid(token);
			Boolean bool = clickRecordService.findRecord(uid, null,nid);
			if(bool){
				return ajaxLotterUser("一个评论一个用户只能点击一次");
			}
			housenoteService.updateNoteTopORFoot( uid,nid, ClickTypeEunm.TOPCLICK.getCode());
		} catch (LotterCusException e) {
			logger.error("房间信息顶一下出错");
			return ajaxLotterServer("服务器异常，请联系管理员");
		}
		return ajaxLotterSuccess();
	}
	
	
	/**
	 * 房间新闻顶一下
	 * @param request
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/footnumNote",method = RequestMethod.GET)
	public Map<String,Object> footnumNote(@RequestParam(value ="token",required = true) String token,
			@RequestParam(value = "nid",required=true) Long nid){
		if(nid == null)
			return ajaxLotterStage("参数有误，请重新请求");
		try {
			
			Housenote housenote =housenoteService.selectByPrimaryKey(nid);
			if(housenote == null){
				return ajaxLotterStage("没有找到相关评论");
			}
			Long uid = StaticDataContext.onLineUid(token);
			Boolean bool = clickRecordService.findRecord(uid, null,nid);
			if(bool){
				return ajaxLotterUser("一个评论一个用户只能点击一次");
			}
			housenoteService.updateNoteTopORFoot(uid, nid, ClickTypeEunm.FOOTCLICK.getCode());
		} catch (LotterCusException e) {
			logger.error("房间信息顶一下出错");
			return ajaxLotterServer("服务器异常，请联系管理员");
		}
		return ajaxLotterSuccess();
	}
	
	
}
